package com.zl.roomdesign.auth

import android.arch.lifecycle.MutableLiveData
import android.util.Log
import com.zl.core.MainApp
import com.zl.core.api.ServiceGenerator
import com.zl.core.base.BaseViewModel
import com.zl.core.db.user.User
import com.zl.core.extend.appSubscribe
import com.zl.core.utils.SPUtils
import kotlin.concurrent.thread

/**
 * Created by zhangli on 2018/5/5,19:54<br/>
 */
class LoginViewModel : BaseViewModel() {

    private val TAG = "LoginViewModel"

    private val mService = ServiceGenerator.createRxService(AuthService::class.java)

    var loginSuccess: MutableLiveData<Boolean> = MutableLiveData()

    fun login(phone: String, pwd: String) {

        mService.login(User(phone.toLong(), pwd, ""))
                .appSubscribe(this, {
                    if (it != null) {
                        SPUtils.saveString(SPUtils.PHONE, phone)
                        loginSuccess.postValue(true)
                        MainApp.instance.user = it
                    }
                    Log.i(TAG, "login: $it")
                })

    }

}